package com.china08.yxyapi.api.home.kecheng.xiaoben;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;

/**
 * 校本课程--播放视频时学习人数加1
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengXiaobenVvAddController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSchoolLessonRepository yxyLessonsSchoolLessonRepository;

    // @Autowired
    // private YxyLessonsUserStudyRepository yxyLessonsUserStudyRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/xiaoben/vv/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        // String userId = getProfile(request, response).getId();
        String lessonId = reqModel.lessonId;

        // YxyLessonsUserStudy yxyLessonsUserStudy =
        // yxyLessonsUserStudyRepository.findByUserIdAndLessonsId(userId,
        // lessonId);
        // if (null == yxyLessonsUserStudy) {
        // yxyLessonsUserStudy = new YxyLessonsUserStudy();
        // yxyLessonsUserStudy.setId(userId + lessonId);
        // yxyLessonsUserStudy.setUserId(userId);
        // yxyLessonsUserStudy.setLessonsId(lessonId);
        // yxyLessonsUserStudyRepository.save(yxyLessonsUserStudy);
        //
        // YxyLessonsSchoolLesson yxyLessonsSchoolLesson =
        // yxyLessonsSchoolLessonRepository.findById(lessonId);
        // if (null != yxyLessonsSchoolLesson) {
        // yxyLessonsSchoolLesson.setVv(yxyLessonsSchoolLesson.getVv() + 1);
        // yxyLessonsSchoolLessonRepository.save(yxyLessonsSchoolLesson);
        // }
        // }

        // 逻辑变更 新修改
        YxyLessonsSchoolLesson yxyLessonsSchoolLesson = yxyLessonsSchoolLessonRepository.findById(lessonId);
        if (null != yxyLessonsSchoolLesson.getVv()) {
            yxyLessonsSchoolLesson.setVv(yxyLessonsSchoolLesson.getVv() + 1);
            yxyLessonsSchoolLessonRepository.save(yxyLessonsSchoolLesson);
        } else {
            yxyLessonsSchoolLesson.setVv(1);
            yxyLessonsSchoolLessonRepository.save(yxyLessonsSchoolLesson);
        }

    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课程ID
        public String lessonId;
    }

}
